package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

//import org.jfree.chart.ChartFactory;
//import org.jfree.chart.ChartFrame;
//import org.jfree.chart.JFreeChart;
//import org.jfree.chart.plot.CategoryPlot;
//import org.jfree.chart.plot.PlotOrientation;
//import org.jfree.data.category.DefaultCategoryDataset;

import Entity.MalfuncExceptReport;
import javax.swing.JComboBox;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;

/**
 * this is the GUI for viewing the malfunction and exeptions report.
 * @author Shahar Shelly
 *
 */
public class ViewMalfunctionExceptionsReport extends JFrame {

	private JPanel contentPane;
	private JLabel lblDateDate;
	private JPanel panel;
	private JTextField textField;
	private JLabel lblTotal;
	private JLabel lblMedian;
	private JTextField textField_1;
	private JButton btnFrequencyDistribution;
	private JTextField textField_2;
	private JTextField textField_3;
	private JLabel lblTotalTimeOf;
	private JTextField textField_4;
	private JLabel lblMedianOfHours;
	private JTextField textField_5;
	private JLabel lblParkingLot;
	private JComboBox<Integer> comboBox;
	private JLabel lblTotalHour;
	private JTextField textField_6;
	private JPanel panel_2;
	private JLabel lblMedian_1;
	private JTextField textField_7;
	private JButton btnReturn;

	/**
	 *  is the constructor. it has a table to chose from and save after validate.
	 */
	public ViewMalfunctionExceptionsReport(MalfuncExceptReport r) {
		final MalfuncExceptReport p2=r;
		setBounds(100, 100, 450, 365);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblPerformanceReport = new JLabel("Malfunction Exceptions Report");
		lblPerformanceReport.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblPerformanceReport.setBounds(11, 11, 253, 24);
		contentPane.add(lblPerformanceReport);
		
		lblDateDate = new JLabel(r.getStart().getDate()+"/"+(r.getStart().getMonth()+1)+"/"+(r.getStart().getYear()+1900)+" - "+r.getEndDate().getDate()+"/"+(r.getEndDate().getMonth()+1)+"/"+(r.getEndDate().getYear()+1900));
		lblDateDate.setBounds(274, 18, 100, 14);
		contentPane.add(lblDateDate);
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Number of Canceled Orders:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(6, 44, 418, 55);
		contentPane.add(panel);
		panel.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(53, 22, 39, 20);
		panel.add(textField);
		textField.setColumns(10);
		int sum=0; for(int i=0; i<6; i++) sum+=r.getCanceled(i+1);
		this.textField.setText(String.valueOf(sum));
		
		lblTotal = new JLabel("Total:");
		lblTotal.setBounds(10, 25, 46, 14);
		panel.add(lblTotal);
		
		lblMedian = new JLabel("Median:");
		lblMedian.setBounds(128, 25, 46, 14);
		panel.add(lblMedian);
		
		textField_1 = new JTextField();
		textField_1.setBounds(184, 22, 39, 20);
		panel.add(textField_1);
		textField_1.setColumns(10);
		this.textField_1.setText(String.valueOf(r.getMedianCancel()));
		
		btnFrequencyDistribution = new JButton("Frequency Distribution");
		btnFrequencyDistribution.addMouseListener(new MouseAdapter() {/*
			@Override
			public void mousePressed(MouseEvent arg0) 
			{
				DefaultCategoryDataset dataset=new DefaultCategoryDataset();
				int[] freq=p2.getCancelFrequencyDist();
				
				for(int i=0; i<freq.length; i++)
					dataset.setValue(freq[i], "Number Of Parking Lot", String.valueOf(i+1));
				
				JFreeChart chart=ChartFactory.createBarChart("Frequency Distribution of Number of Subscriptions","Number Of Subscriptions", "Number Of Parking Lot", dataset,PlotOrientation.VERTICAL,false,true,false);
				CategoryPlot p=chart.getCategoryPlot();
				p.setRangeGridlinePaint(Color.black);
				ChartFrame frame=new ChartFrame("Frequency Distribution",chart);
				frame.setVisible(true);
				frame.setSize(450, 350);
			}*/
		});
		btnFrequencyDistribution.setBounds(258, 19, 150, 23);
		panel.add(btnFrequencyDistribution);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		panel_1.setBorder(new TitledBorder(null, "Number of Latenesses:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(6, 125, 418, 157);
		contentPane.add(panel_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(53, 22, 39, 20);
		panel_1.add(textField_2);
		int sum1=0; for(int i=0; i<6; i++) sum1+=r.getLateness(i+1);
		this.textField_2.setText(String.valueOf(sum1));
		
		JLabel label = new JLabel("Total:");
		label.setBounds(10, 25, 46, 14);
		panel_1.add(label);
		
		JLabel label_1 = new JLabel("Median:");
		label_1.setBounds(128, 25, 46, 14);
		panel_1.add(label_1);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(184, 22, 39, 20);
		panel_1.add(textField_3);
		this.textField_3.setText(String.valueOf(r.getLatenessMedian()));
		
		JButton button = new JButton("Frequency Distribution");
		button.addMouseListener(new MouseAdapter() {/*
			@Override
			public void mousePressed(MouseEvent arg0) 
			{
				DefaultCategoryDataset dataset=new DefaultCategoryDataset();
				int[] freq=p2.getLatenessFrequencyDist();
				
				for(int i=0; i<freq.length; i++)
					dataset.setValue(freq[i], "Number Of Parking Lot", String.valueOf(i+1));
				
				JFreeChart chart=ChartFactory.createBarChart("Frequency Distribution of Number of Latenesses","Number Of Latenesses", "Number Of Parking Lot", dataset,PlotOrientation.VERTICAL,false,true,false);
				CategoryPlot p=chart.getCategoryPlot();
				p.setRangeGridlinePaint(Color.black);
				ChartFrame frame=new ChartFrame("Frequency Distribution",chart);
				frame.setVisible(true);
				frame.setSize(450, 350);
			}*/
		});
		button.setBounds(258, 19, 150, 23);
		panel_1.add(button);
		
		lblTotalTimeOf = new JLabel("Total hours of latenesses:");
		lblTotalTimeOf.setBounds(10, 62, 134, 14);
		panel_1.add(lblTotalTimeOf);
		
		textField_4 = new JTextField();
		textField_4.setBounds(155, 59, 55, 20);
		panel_1.add(textField_4);
		textField_4.setColumns(10);
		int sum2=0; for(int i=0; i<6; i++) sum2+=r.NumOfLateHours(i+1);
		textField_4.setText(String.valueOf(sum2));
		
		lblMedianOfHours = new JLabel("Median of hours:");
		lblMedianOfHours.setBounds(242, 62, 99, 14);
		panel_1.add(lblMedianOfHours);
		
		textField_5 = new JTextField();
		textField_5.setBounds(336, 59, 55, 20);
		panel_1.add(textField_5);
		textField_5.setColumns(10);
		textField_5.setText(String.valueOf(r.NumOfLateHoursMed()));
		
		panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Latenesses Details For Specific Parking Lot:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(10, 96, 398, 57);
		panel_1.add(panel_2);
		panel_2.setLayout(null);
		
		textField_6 = new JTextField();
		textField_6.setBounds(226, 22, 39, 20);
		panel_2.add(textField_6);
		textField_6.setColumns(10);
		
		lblParkingLot = new JLabel("Parking Lot:");
		lblParkingLot.setBounds(10, 25, 82, 14);
		panel_2.add(lblParkingLot);
		
		lblTotalHour = new JLabel("Total hours:");
		lblTotalHour.setBounds(147, 25, 72, 14);
		panel_2.add(lblTotalHour);
		
		lblMedian_1 = new JLabel("Median:");
		lblMedian_1.setBounds(284, 25, 55, 14);
		panel_2.add(lblMedian_1);
		
		textField_7 = new JTextField();
		textField_7.setColumns(10);
		textField_7.setBounds(349, 22, 39, 20);
		panel_2.add(textField_7);
		
		comboBox = new JComboBox<Integer>();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) 
			{
				textField_6.setText(String.valueOf(p2.NumOfLateHours((int) comboBox.getSelectedItem())));
				textField_7.setText(String.valueOf(p2.getLengthOfLateMedForLot((int)comboBox.getSelectedItem())));
			}
		});
		comboBox.setBounds(80, 22, 39, 20);
		panel_2.add(comboBox);
		
		btnReturn = new JButton("return");
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		btnReturn.setBounds(152, 293, 89, 23);
		contentPane.add(btnReturn);
		for(int k=0; k<6; k++) comboBox.addItem(k+1);
	}
	
	public void close()
	{
		WindowEvent windowclose=new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowclose);
	}
}
